<?php

function list_nipar(){
	$header = array(
		'#'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'Gol'=>array('style'=>'text-align:left;border-bottom:2px solid;width:10%'),  
		'Grade'=>array('style'=>'text-align:left;border-bottom:2px solid;width:25%'),  
		'Sumber'=>array('style'=>'text-align:right;border-bottom:2px solid;width:25%'),  
		'Pendukung'=>array('style'=>'text-align:right;border-bottom:2px solid;width:25%'),  
		' '=>array('style'=>'border-bottom:2px solid;width:10%'), 
	);
	$query = "SELECT a.*,b.sumber,b.pendukung FROM golongan_jabatan a 
		LEFT JOIN nipar b ON a.golongan_jabatan_id=b.golongan_jabatan_id 
		ORDER BY a.golongan_jabatan_id ASC";
	$result = my_query($query);
	$i=0;
	$row = array();
	while($ey = my_fetch_array($result) ){
		$query = "SELECT MIN(grade_id) as min, MAX(grade_id) as max FROM gol_grade where golongan_jabatan_id=".$ey['golongan_jabatan_id'];
		$re = my_query($query);
		$grades = my_fetch_array($re);
		
		$i++;
		$editproperty = array(
			'href'=>'index.php?com=nipar&task=edit&id='.$ey['golongan_jabatan_id'] ,
			'title'=>'Edit',
			'rel'=>'facebox'
		);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );

		$row[] = array(
			'#'=>position_text_align ($i, 'center'),    
			'gol'=>  $ey['kode_golongan_jabatan'] ,     
			'grade'=>  sprintf('%02d' ,$grades['min']).' - '.sprintf('%02d' ,$grades['max']) ,     
			'sumber'=>  position_text_align(  $ey['sumber'] ,     'right'),
			'pendukung'=>  position_text_align( $ey['pendukung'],'right'),
			'operasi'=> position_text_align( $edit_button , 'right'),
		);
	}
	$datas = table_rows($row); 
	return  $view.'<br/>'. table_builder($header , $datas ,  5 , false );
/*	
	$view = form_header( "pr_komponen_jabatan" , "pr_komponen_jabatan"  );
	$view .= form_footer( );
	return  $view.'<br/>'. table_builder($header , $datas ,  5 , false );
*/	
}
function form_nipar_create($id){
  	
	$view = form_header( "nipar" , "nipar"  );
 	$fields = my_get_data_by_id('nipar','golongan_jabatan_id',$id); 
	$golongan_jabatan = my_get_data_by_id('golongan_jabatan','golongan_jabatan_id',$id);

	$sumber = array(
		'name'=>'sumber',
		'value'=>( isset($_POST['sumber']) ? $_POST['sumber'] :$fields['sumber'] ),
		'id'=>'sumber',
		'type'=>'text',
		'size'=>'15'
	);
	$form_field_sumber = form_dynamic($sumber); 
	$view .= form_field_display( $form_field_sumber   , "Sumber"   );

	$pendukung = array(
		'name'=>'pendukung',
		'value'=>( isset($_POST['pendukung']) ? $_POST['pendukung'] :$fields['pendukung'] ),
		'id'=>'pendukung',
		'type'=>'text',
		'size'=>'15'
	);
	$form_field_pendukung= form_dynamic($pendukung); 
	$view .= form_field_display( $form_field_pendukung   , "Pendukung"   );
	
	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
}


function form_nipar_submit($id = 0){
	$nipar=my_get_data_by_id('nipar','golongan_jabatan_id',$id);
	$datas = array(
		'golongan_jabatan_id'=>my_type_data_int($id), 		 	 	 	 	 	 	 
		'sumber'=>my_type_data_str($_POST['sumber']), 		 	 	 	 	 	 	 
		'pendukung'=>my_type_data_str($_POST['pendukung']), 			
	);
	if($nipar){
		return my_update_record('nipar','golongan_jabatan_id',$id, $datas);
	}
	return my_insert_record('nipar' , $datas);
}

function form_nipar_validate($id){
	$errsubmit = false;
	$err = array();
    
	if(! is_numeric(trim($_POST['sumber']) )){
		$errsubmit = true;
		$err[] = "Data sumber bukan angka";
	}
	if(! is_numeric(trim($_POST['pendukung']) )){
		$errsubmit = true;
		$err[] = "Data pendukung bukan angka";
	}
 

	if( $errsubmit){
		return $err;
	} 
	return $errsubmit;
}